<?php
    class App extends CActiveRecord
    {
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        public function tableName() {                  
            return 'c_app';
        }
        /* Lấy app clip */
        public function getAppClip()
        {
            $sql = "SELECT id, title,introtext, alias,picture,download FROM c_app WHERE  id = 3";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();
            return $rows;
        }
        /* Lấy app khác */
        public function getOtherApp($currentPage, $numberRecordPerPage)
        {
            $cacheService = new CacheService("App","getOtherApp",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key); 
            //$cache = false;
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
                $sql = "SELECT id, title,introtext, alias,picture,download,create_date FROM c_app WHERE  id != 3 AND status =1 ORDER BY position ASC  LIMIT " . $startRecord . ", " . $numberRecordPerPage;
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryAll();
                Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_7200); 
            }else{ 
                $rows=$cache;  
            }
            return $rows;
        }
        public function getOtherAppHot($currentPage, $numberRecordPerPage)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
            $sql = "SELECT id, title,introtext, alias,picture,download,create_date FROM c_app WHERE  id != 3 AND status =1 ORDER BY download DESC  LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();   
            return $rows;
        }

        public function getTotalApp()
        {
            $sql = "SELECT count(id) FROM c_app WHERE  id != 3 AND status =1 ";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        /* Thông tin về ứng dụng */
        public function getInfoApp($id)
        {
            $sql = "SELECT * FROM c_app WHERE id=" . $id;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();  
            return $rows;   
        }
        public function getRandomApp(){
            $connect =Yii::app()->db;
            $sql="SELECT * FROM c_app WHERE status=1 ORDER BY RAND() LIMIT 0,5";
            $command=$connect->createCommand($sql);
            $rows= $command->queryAll();
            return $rows;
        }

    } 
?>
